package com.edu.medicalpro.activity;

import android.content.Intent;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.edu.medicalpro.R;
import com.edu.medicalpro.base.BaseActivity;
import com.edu.medicalpro.kepu.KePuFragment;
import com.edu.medicalpro.mine.InfoFragment;
import com.edu.medicalpro.mvp.view.HomeFragement;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends BaseActivity implements
        BottomNavigationView.OnNavigationItemSelectedListener {

    private BottomNavigationView bottomNavigationView;

    private Fragment[] fragments ;

    private int mDefaultIndex =0;

    @Override
    protected void initView() {
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
        fragments = new Fragment[]{new HomeFragement(),
                new KePuFragment(),new InfoFragment()};
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragement,fragments[0])
                .commit();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull  MenuItem item) {
        item.setChecked(true);
        switch (item.getItemId()) {
            case R.id.main_bottom:
                switchFragment(0);
                break;
            case R.id.kepu_bottom:
                switchFragment(1);
                break;
            case R.id.mine_bottom:
                switchFragment(2);
                break;
        }
        return false;
    }

    public void switchFragment(int to){
        if (mDefaultIndex == to){
            return;
        }
        FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        if(!fragments[to].isAdded()){
            fragmentTransaction.add(R.id.fragement,fragments[to]);
        }else{
            fragmentTransaction.show(fragments[to]);
        }
        fragmentTransaction.hide(fragments[mDefaultIndex])
        .commitAllowingStateLoss();
        mDefaultIndex =to;
    }

    public void YiYuanInfo(View view) {
        Intent intent=new Intent(MainActivity.this,YiYuanInfoActivity.class);
        startActivity(intent);
    }

    public void KeShiInfo(View view) {
        Intent intent=new Intent(MainActivity.this,KeShiInfoActivity.class);
        startActivity(intent);
    }

    public void ZhuanJiaInfo(View view) {
        Intent intent=new Intent(MainActivity.this,ZhuanJiaInfoActivity.class);
        startActivity(intent);
    }

    public void YuYueGuaHao(View view) {
        Intent intent=new Intent(MainActivity.this,GuaHaoActivity.class);
        startActivity(intent);
    }

    public void MenZhenPay(View view) {
        Intent intent=new Intent(MainActivity.this,MenZhenPayActivity.class);
        startActivity(intent);
    }

    public void MenZhenFind(View view) {
        Intent intent=new Intent(MainActivity.this,MenZhenFindActivity.class);
        startActivity(intent);
    }

    public void ZhiNengHouZhen(View view) {
        Intent intent=new Intent(MainActivity.this,HouZhenActivity.class);
        startActivity(intent);
    }


}